#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

/* int totalPrice = 0; */

class Computer
{
public:
    Computer(const char * brand,double price)
    : _brand(new char[strlen(brand) + 1]())
    , _price(price)
    {
        _totalPrice += _price;
        strcpy(_brand,brand);
        cout << "Computer(const char *,double)" << endl;
    }


    ~Computer(){
        cout << "~Computer()" << endl;
        if(_brand){
            _totalPrice -= _price;
            delete [] _brand;
            _brand = nullptr;
        }
    }

    Computer(const Computer & rhs)
    : _brand(new char[strlen(rhs._brand) + 1]())
    , _price(rhs._price)
    {
        _totalPrice += _price;
        strcpy(_brand,rhs._brand);
        cout << "拷贝构造" << endl;
    }

    Computer & operator=(const Computer & rhs){
        cout << "赋值运算符函数" << endl;
        if(this != &rhs){
            _totalPrice -= _price;
            delete [] _brand;
            _brand = new char[strlen(rhs._brand) + 1]();
            strcpy(_brand,rhs._brand);
            _price = rhs._price;
            _totalPrice += _price;
        }
        return *this;
    }

    void print() {
        cout << "brand:" << _brand << endl;
        cout << "price:" << _price << endl;
        /* cout << "totalPrice:" << _totalPrice << endl; */
    }

    static void printTotalPrice(){
        /* Computer pc("apple",20000); */
        //error
        /* this->print(); */
        /* cout << "price:" << this->_price << endl; */
        cout << "totalPrice:" << _totalPrice << endl;
    }
private:
    char * _brand;
    double _price;
    static double _totalPrice;
};
double Computer::_totalPrice = 0;


void test0(){
    cout << sizeof(Computer) << endl;
    Computer pc("Apple",20000);
    Computer pc2("Xiaomi",8000);
    Computer pc3("Huawei",21000);
    /* cout << pc._totalPrice << endl; */
    /* cout << pc2._totalPrice << endl; */
    /* cout << pc3._totalPrice << endl; */

    //静态成员通过类名+作用域限定的方式访问
    //是更常见的访问方式
    /* cout << Computer::_totalPrice << endl; */

    /* cout << totalPrice << endl; */
    /* pc.printTotalPrice(); */
    Computer::printTotalPrice();

}

int main(void){
    test0();
    return 0;
}
